using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using Random = UnityEngine.Random;

public class MJCS : MonoBehaviour
{
    //属于什么组
    // private MJGroup mMJGroup;
    //一共九章或者 东南西北中发白 春夏秋冬梅兰竹菊     是第几章
    public int mMJCardNum;
    //一共四张，是第几章的索引
    public int mMJCardNumIndex;
    private bool misLive = false;
    private Transform mNormalParent;
    private Transform mTableParent;
    private Vector3 mLocalScale = Vector3.one;
    
    public BoxCollider mboxCollider;
    private MeshRenderer mMeshRender;
    public Rigidbody mrightbody;

    public void OnInit(Transform tras,Transform table,Material met,int cardnum,int numindex)//MJGroup group,
    {
        mboxCollider = transform.GetComponent<BoxCollider>();
        mMeshRender = transform.Find("MaJiang").GetComponent<MeshRenderer>();
        mrightbody = transform.GetComponent<Rigidbody>();
        if (tras != null)
        {
            mNormalParent = tras;
            mTableParent = table;
            transform.SetParent(tras);
        }
        transform.position = Vector3.zero;
        transform.localScale = Vector3.one;
        transform.localRotation = Quaternion.identity;
        gameObject.SetActive(false);

        if (met != null)
        {
            mMeshRender.material = met;
        }
        // mMJGroup = group;
        mMJCardNum = cardnum;
        mMJCardNumIndex = numindex;
    }

    //设置一个mat
    public void OnSetMetail(Material met)
    {
        if (met != null)
        {
            mMeshRender.material = met;
        }
    }
    
    public void OnSetSale(float sacle)
    {
        mLocalScale = Vector3.one*sacle;
        transform.localScale = mLocalScale;
    }
    
    public GameObject OnGetObj()
    {
        return gameObject;
    }
    
    //添加一个力
    public void AddFace(float x,float z,float hight)
    {
        transform.SetParent(mTableParent);
        gameObject.SetActive(true);
        OnZhengli();
        transform.position = new Vector3(0, hight, 0);
        misLive = true;
        
        transform.localRotation = Quaternion.identity;
        mrightbody.AddForce(new Vector3(x,0,z));
    }

    public void DoRandomEulerAngles()
    {
        transform.localEulerAngles = new Vector3(0, Random.Range(-15, 15),0 );
    }

    //全部都正面朝上
    public void OnZhengli()
    {
        transform.localRotation = Quaternion.identity;
    }

    //获取这张牌是否还存在
    public bool GetLiveStatus()
    {
        return misLive;
    }

    //设置缩放
    public void DoSetScale(float scale)
    {
        transform.DOScale(Vector3.one * scale, 0.1f).SetEase(Ease.InSine).OnComplete(() =>
        {
            transform.localScale = mLocalScale;
        });
    }
    //移动到指定位置
    public void DoMoveToPos(Vector3 endpos,TweenCallback callback=null)
    {
        mboxCollider.enabled = false;
        transform.DOLocalRotate(Vector3.zero, 0.2f);
        transform.DOMove(endpos, 0.1f).SetEase(Ease.InSine).OnComplete(() =>
        {
            mboxCollider.enabled = true;
            if (callback != null)
            {
                callback();
            }
        });
    }
    
    public void DoMovePosToFapai(Vector3 endpos,float duration = 0.2f,float waitfortime = 0.1f)
    {
        OnRightBodyPositoionDown(true);
        // OnLockRightBodyPositoion(true);
        mboxCollider.enabled = false;
        transform.DOLocalRotate(Vector3.zero, duration);
        transform.DOMove(endpos, duration).SetDelay(waitfortime).SetEase(Ease.Linear).OnComplete(() =>
        {
            mboxCollider.enabled = true;
        });
    }
    
    public void DoMoveZero()
    {
        mboxCollider.enabled = false;
        // DoSetScale(0.7f);
        transform.DOLocalRotate(Vector3.zero, 0.2f);
        transform.DOMove(new Vector3(0,2,0), 0.2f).SetDelay(Random.Range(0,0.2f)).SetEase(Ease.Linear).OnComplete(() =>
        {
            transform.gameObject.SetActive(false);
        });
    }
    
    //洗牌的时候需要关闭所有的 牌，并回到之前的状态
    public void OnSetDisLive()
    {
        misLive = false;
        gameObject.SetActive(false);
        transform.SetParent(mNormalParent);
    }

    public void OnSetLive()
    {
        misLive = true;
        gameObject.SetActive(true);
        transform.SetParent(mTableParent);
        OnZhengli();
        transform.localScale = mLocalScale;
    }


    //显示牌面
    public void OnShowCardFase(Action callback = null)
    {
        OnLockRightBodyPositoion(true);
        transform.DOLocalRotate(new Vector3(0, 0, 180), 0.2f).OnComplete(() =>
        {
            OnLockRightBodyPositoion(false);
            if (callback != null)
            {
                callback();
            }
        });
    }
    public void OnShowCardFaseEx(Action callback = null)
    {
        Vector3 startpos = transform.localPosition;

        transform.DOLocalMoveY(startpos.y + 0.6f, 0.2f).SetEase(Ease.OutBack).OnComplete(() =>
        {
            transform.DOLocalRotate(Vector3.zero, 0.2f).OnComplete(() =>
            {
                transform.DOLocalMoveY(startpos.y, 0.1f).SetEase(Ease.InBack).OnComplete(() =>
                {
                    int wait = 0;
                    DOTween.To(() => wait, x => wait = x, 10, 0.2f).OnComplete(() =>
                    {
                        // transform.localPosition = startpos;
                        if (callback != null)
                        {
                            callback();
                        }
                    });
                });
            });
        });
        // transform.DOLocalRotate(new Vector3(0, 0, isface?0:180), 0.2f)
    }
    public void OnShowCardBe(float x,float z,float hight = 0.4f,bool iscardback = true)
    {
        // mrightbody.constraints = RigidbodyConstraints.FreezePosition;
        misLive = true;
        gameObject.SetActive(true);
        transform.SetParent(mTableParent);
        transform.localPosition = new Vector3(x, hight, z);
        transform.localEulerAngles = new Vector3(0, 0, iscardback?180:0);
        
    }

    public void OnLockRightBodyPositoion(bool islock)
    {
        mrightbody.constraints = islock ? RigidbodyConstraints.FreezePosition:RigidbodyConstraints.None;
    }
    public void OnLockRightBodyRotation(bool islock)
    {
        mrightbody.constraints = islock ? RigidbodyConstraints.FreezeRotation:RigidbodyConstraints.None;
    }
    public void OnRightBodyPositoionDown(bool isdowndrag)
    {
        int drag = 0;
        if (isdowndrag)
        {
            int allmjcount = GameHelp.Game1SendCardCountList[GameHelp.GameLevel];
            if (allmjcount >= 40)
            {
                drag = 10;
            }
            if (allmjcount >= 30)
            {
                drag = 8;
            }
            if (allmjcount >= 4)
            {
                drag = 5;
            }
        }
        mrightbody.drag = drag;
    }
}
